import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/attention_room_request.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/model/anchor.dart';
import 'package:elephant_fish/model/anchor_entity.dart';
import 'package:elephant_fish/page/login_page.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SearchListCard extends StatefulWidget {
  final AnchorEntity anchor;
  const SearchListCard({Key key, this.anchor}) : super(key: key);

  @override
  _SearchListCardState createState() => _SearchListCardState();
}

class _SearchListCardState extends State<SearchListCard> {
  @override
  Widget build(BuildContext context) {
    String anchorId = 'ID:' + widget.anchor.id.toString();
    return Stack(
      children: [
        Align(
          alignment: Alignment.topLeft,
          child: Padding(padding: EdgeInsets.only(left: SizeUtil.px(18),top: SizeUtil.px(24)),
            child:  CircleAvatar(
              radius: SizeUtil.px(35),
              backgroundImage: NetworkImage('http://119.28.131.94:82' + widget.anchor.avatar),
              child: Container(
                width: SizeUtil.px(70),
                height: SizeUtil.px(70),
              ),
            ),
          ),
        ),
        widget.anchor.broadcastStatus == 1 ?
        Align(
          alignment: Alignment.topLeft,
          child:  Padding(padding: EdgeInsets.only(left: SizeUtil.px(26),top: SizeUtil.px(84)),
            child: Container(
              width: SizeUtil.px(62),
              height: SizeUtil.px(20),
              child: Image.asset('assets/images/unplaying.png'),
            ),
          ),
        ) :
        Align(
          alignment: Alignment.topLeft,
          child:  Padding(padding: EdgeInsets.only(left: SizeUtil.px(26),top: SizeUtil.px(84)),
            child: Container(
              width: SizeUtil.px(62),
              height: SizeUtil.px(20),
              child: Image.asset('assets/images/playing.png'),
            ),
          ),
        ),
        Align(
          alignment: Alignment.topLeft,
          child:  Padding(padding: EdgeInsets.only(left: SizeUtil.px(134)),
              child:Stack(
                children: [
                  Padding(padding: EdgeInsets.only(top: SizeUtil.px(31)),
                    child:   Text(
                      widget.anchor.nickname,
                      style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: SizeUtil.px(67)),
                    child:  Text(
                      anchorId,
                      style: TextStyle(fontSize: SizeUtil.px(22),color: Color(0xffbebebe)),
                    ),
                  )
                ],
              )
          ),
        ),
        Align(
          alignment: Alignment.topLeft,
          child: Padding(
            padding: EdgeInsets.only(top: SizeUtil.px(121)),
            child: Divider(
              height: SizeUtil.px(1),
              indent: SizeUtil.px(135),
              color: Color(0xfff3f3f3),
            ),
          ),
        ),
        widget.anchor.isAttention ?
        Align(
          alignment: Alignment.topRight,
          child: Padding(
            padding: EdgeInsets.only(right: SizeUtil.px(28),top: SizeUtil.px(40)),
            child: Container(
              width: SizeUtil.px(106),
              child: InkWell(
                child: Image.asset('assets/images/followed.png'),
                onTap: (){
                  TxUtils.showToast('您已关注该主播', context);
                },
              ),
            ),
          ),
        ) :
        Align(
          alignment: Alignment.topRight,
          child: Padding(
            padding: EdgeInsets.only(right: SizeUtil.px(28),top: SizeUtil.px(40)),
            child: Container(
              width: SizeUtil.px(106),
              child: InkWell(
                child: Image.asset('assets/images/follow.png'),
                onTap: (){
                  attentionAnchor();
                },
              ),
            ),
          ),
        )
      ],
    );
  }

  ///关注主播
  Future<void> attentionAnchor() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = AttentionRoomRequest();
    request
        .add('user_id',  widget.anchor.userId)
        .add('type', 2)
        .addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == 0) {
      TxUtils.showToast(result['data'], context);
      setState(() {
        widget.anchor.isAttention = true;
      });
    } else if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(
            arguments: {'loginCode': '-1'},
          );
        },
      ));
    } else {
      TxUtils.showToast(result['message'], context);
    }
  }
}

